package com.zdw.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.zdw.common.config.thread.CommonThreadPoolConfig;
import com.zdw.common.parent.JSONResult;
import com.zdw.demo.config.international.InternationalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2022-11-21
 */
@Slf4j
@RestController
@RequestMapping("/jdk11")
@Api(value = "jdk11测试接口", tags = {"jdkApi"})
public class Jdk11TestController {

    @ApiOperation(value = "jdk11测试-1", consumes = "application/json;charset=UTF-8")
    @PostMapping(value = "/testJdk11")
    public JSONResult testJdk() throws IOException, InterruptedException, ExecutionException {
        //1.特性1:var自动判断变量类型(成员变量不允许)
        var a = 10;
        var b = " STR ING ";

        //2.特性2:文件流快速复制-transferTo
        FileInputStream  fis = new FileInputStream("D:\\环境和软件\\1.总结\\Markdown\\10.Mysql.md");
        FileOutputStream fos = new FileOutputStream("D:\\环境和软件\\1.总结\\Markdown\\Mysql-copy.md");
        fis.transferTo(fos);

        //3.特性3:Files类方法readString()-用于快速读取文件的内容;方法writeString()-用于快速写出一个字符串数据到文件
        Path   path1      = Paths.get("D:\\环境和软件\\1.总结\\Markdown\\10.Mysql.md");
        String readString = Files.readString(path1);
        System.out.println("读取文件readString:" + readString);
        Path path2 = Paths.get("D:\\环境和软件\\1.总结\\Markdown\\Mysql-copy.md");
        Files.writeString(path2, "JDK11-测试添加字符串到文件(会覆盖原文件)");

        //4.特性4:String提供了isBlank()，strip(), stripLeading(), stripTrailing()等方法
        boolean isBlank       = b.isBlank();
        String  isStrip       = b.strip();
        String  stripLeading  = b.stripLeading();
        String  stripTrailing = b.stripTrailing();
        String  repeat        = b.repeat(3);
        System.out.println("是否为空isBlank:" + isBlank);
        System.out.println("去掉前后空格isStrip:" + isStrip);
        System.out.println("去掉前空格stripLeading:" + stripLeading);
        System.out.println("去掉后空格stripTrailing:" + stripTrailing);
        System.out.println("字符复制N次后组成新词repeat:" + repeat);

        //5.特性5:针对单列集合以及双列集合的增强(LIST/SET/MAP)
        //[1]集合不可增加/删除/修改等操作
        List<String> list1 = List.of("abc1", "bbc2", "cbc1");
        System.out.println("of方法定义集合:" + list1);
        //[2]集合不可增加/删除/修改等操作 [2]浅拷贝,前后两个集合相互影响
        List<String> list2 = List.copyOf(list1);
        System.out.println("copyOf方法复制集合" + list2);

        //6.特性6:Stream中新增了takeWhile()和dropWhile()截止结算的方法
        List<String> dropWhile = list1.stream().dropWhile(x -> x.contains("a")).collect(Collectors.toList());
        List<String> takeWhile = list2.stream().takeWhile(x -> x.contains("1")).collect(Collectors.toList());
        System.out.println("dropWhile方法去掉集合中带'a'的元素,直到第一个不满足为止" + dropWhile);
        System.out.println("takeWhile方法去掉集合中带'1'的元素,直到第一个不满足为止" + takeWhile);

        //7.特性7:HttpClient加强方法
        HttpClient                       client    = HttpClient.newHttpClient();
        HttpRequest                      request1  = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8082/jdk11/test")).build();
        HttpResponse.BodyHandler<String> handler   = HttpResponse.BodyHandlers.ofString();
        HttpResponse<String>             response1 = client.send(request1, handler);
        String                           body1     = response1.body();
        System.out.println("同步调用send:" + body1);

        HttpRequest                             request2  = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8082/jdk11/test")).build();
        CompletableFuture<HttpResponse<String>> response2 = client.sendAsync(request2, handler);
        HttpResponse<String>                    result    = response2.get();
        String                                  body2     = result.body();
        System.out.println("异步调用sendAsync:" + body2);

        //8.其他:垃圾回收ZGC(试验阶段)/新的Epsilon垃圾收集器/全面支持Linux容器(如:Docker)/Unicode10增加字符

        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.query.success"), "JDK11测试-1");
    }

    @ApiOperation(value = "jdk11测试-2", consumes = "application/json;charset=UTF-8")
    @GetMapping(value = "/test11")
    public JSONResult test() {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.query.success"), "JDK11测试-2");
    }

    /**
     * 测试通用线程池使用
     */
    @ApiOperation(value = "线程池测试", consumes = "application/json;charset=UTF-8")
    @PostMapping(value = "/testThreadPool", produces = "application/json")
    public void testThreadPool() {
        //拿取线程池对象
        ThreadPoolExecutor instance = CommonThreadPoolConfig.getInstance();
        for (int i = 0; i < 10; i++) {
            String str = i + 1 + StrUtil.EMPTY;
            instance.execute(() -> threadMethod(str));
        }
    }

    /**
     * 假设为每个线程都要需要的操作
     *
     * @param str
     * @return
     */
    private String threadMethod(String str) {
        System.out.println(str);
        return str;
    }

}
